package model

import (
	"blog/global"
	"blog/model/response"
	"gorm.io/gorm"
)

type Article struct {
	Category Category `gorm:"foreignkey:Cid"`
	gorm.Model
	Title        string `gorm:"type:varchar(100);not null" json:"title"`
	Cid          int    `gorm:"type:int;not null" json:"cid"`
	Desc         string `gorm:"type:varchar(200)" json:"desc"`
	Content      string `gorm:"type:longtext" json:"content"`
	Img          string `gorm:"type:varchar(100)" json:"img"`
	CommentCount int    `gorm:"type:int;not null;default:0" json:"comment_count"`
	ReadCount    int    `gorm:"type:int;not null;default:0" json:"read_count"`
}

//添加文章
func CreateArticle(article *Article) int {
	err := global.DB.Create(&article).Error
	if err != nil {
		return response.ERROR
	}
	return response.SUCCESS

}

//查询分类文章
func GetCategoryArticles(id int, pageSize int, pageNum int) ([]Article, int, int64) {
	var cateArtList []Article
	var total int64
	var err error
	if pageNum == 0 {
		err = global.DB.Preload("Category").Limit(pageSize).Offset(-1).Where("cid =?", id).Find(&cateArtList).Error
	} else {
		err = global.DB.Preload("Category").Limit(pageSize).Offset((pageNum-1)*pageSize).Where("cid =?", id).Find(&cateArtList).Error
	}

	global.DB.Model(&cateArtList).Where("cid =?", id).Count(&total)
	if err != nil {
		return nil, response.ERROR_CATE_NOT_EXIST, 0
	}
	return cateArtList, response.SUCCESS, total
}

//查询单个文章
func GetArticleInfo(id int) (Article, int) {
	var article Article
	err := global.DB.Where("id = ?", id).Preload("Category").First(&article).Error
	global.DB.Model(&article).Where("id = ?", id).UpdateColumn("read_count", gorm.Expr("read_count + ?", 1))
	if err != nil {
		return article, response.ERROR_ART_NOT_EXIST
	}
	return article, response.SUCCESS
}

//查询文章列表
func GetArticles(pageSize int, pageNum int) ([]Article, int, int64) {
	var articles []Article
	var err error
	var total int64
	if pageNum == 0 {
		err = global.DB.Select("article.id, title, img, created_at, updated_at, `desc`, comment_count, read_count, category.name").Limit(pageSize).Offset(-1).Order("Created_At DESC").Joins("Category").Find(&articles).Error
	} else {
		err = global.DB.Select("article.id, title, img, created_at, updated_at, `desc`, comment_count, read_count, category.name").Limit(pageSize).Offset((pageNum - 1) * pageSize).Order("Created_At DESC").Joins("Category").Find(&articles).Error
	}
	global.DB.Model(&articles).Count(&total)
	if err != nil && err != gorm.ErrRecordNotFound {
		return nil, response.ERROR, 0
	}
	return articles, response.SUCCESS, total
}

//编辑文章
func EditArticle(id int, article *Article) int {
	var maps = make(map[string]interface{})
	maps["title"] = article.Title
	maps["cid"] = article.Cid
	maps["desc"] = article.Desc
	maps["content"] = article.Content
	maps["img"] = article.Img
	err := global.DB.Model(&Article{}).Where("id=?", id).Updates(maps).Error
	if err != nil {
		return response.ERROR
	}
	return response.SUCCESS
}

//删除文章
func DeleteArticle(id int) int {
	var article Article
	err := global.DB.Where("id=?", id).Delete(&article).Error
	if err != nil {
		return response.ERROR
	}
	return response.SUCCESS
}
